/**
 * @Date 2023-02-21 09:42:02
 * @Remark
 */

// lib
import _ from "lodash";
// config
// script & methods & public
import logUtils from "utils/logUtils";
// state
import { dataState } from "state/global";
// http
import { open, bag, isOkRes } from "public/http/api";
// service
import {
  simpleListService,
  simplePageService,
  simpleResService,
} from "public/service/simple.service";
// api
import {
  ReqRolePage,
  API_ROLE_PAGE,
  ReqRoleAdd,
  API_ROLE_ADD,
  API_ROLE_UPDATE,
  ReqRoleUpdate,
  API_ROLE_DEL,
  // API_ROLE_MENU,
  API_ROLE_PERMISSION,
  // ReqRoleMenu,
  // API_ROLE_MENU_ADD,
  API_ROLE_LIST,
  // ReqRoleMenuEdit,
  // DataRoleMenu,
  // API_ROLE_MENU_DEL,
  ReqRolePermission,
  API_ROLE_PERMISSION_ADD,
  API_ROLE_PERMISSION_DEL,
  ReqRolePermissionEdit,
  ReqRoleDel,
  API_PERMISSION_LIST,
} from "./api";
// interface
// 其它

// 角色分页
export async function getRolePageService(params: ReqRolePage) {
  return simplePageService<ReqRolePage, RoleDto>(API_ROLE_PAGE, params);
}

// 角色列表
export async function getRoleListService() {
  const result = await simpleListService<any, RoleDto>(API_ROLE_LIST, {});
  if (result.payload) {
    dataState.roles = result.payload;
  }
  return result;
}

// 角色添加
export async function addRoleService(params: ReqRoleAdd) {
  return simpleResService<ReqRoleAdd>(API_ROLE_ADD, params);
}

// 角色编辑
export async function updateRoleService(params: ReqRoleUpdate) {
  return simpleResService<ReqRoleUpdate>(API_ROLE_UPDATE, params);
}

// 角色删除
export async function delRoleService(id: string) {
  return simpleResService<ReqRoleDel>(API_ROLE_DEL, {
    id,
  });
}

// 获取角色权限
export async function getRolePermissionService(id: string) {
  return simpleListService<ReqRolePermission, RoleDto>(API_ROLE_PERMISSION, {
    id: id,
  });
}

// 获取角色菜单
// export async function getRoleMenuService(id: string) {
//   return simpleListService<ReqRoleMenu, DataRoleMenu>(API_ROLE_MENU, {
//     roleId: id,
//   });
// }

// 编辑角色权限
export async function editRolePermissionService(
  id: string,
  have: string[],
  selected: string[],
) {
  const add = _.difference(selected, have);
  const del = _.difference(have, selected);
  logUtils.info("增加角色权限", add);
  logUtils.info("删除角色权限", del);
  if (add.length > 0) {
    // 打开接口
    const a1 = await open<ReqRolePermissionEdit, ResBoolean>(
      API_ROLE_PERMISSION_ADD,
      {
        id,
        permissionId: add[0],
      },
    );
    // 创建返回数据
    const data = bag<boolean>(a1);
    // 特殊处理
    if (isOkRes(a1)) {
      data.payload = true;
    }
    return data;
  }
  if (del.length > 0) {
    // 打开接口
    // 创建返回数据
    const a1 = await open<ReqRolePermissionEdit, ResBoolean>(
      API_ROLE_PERMISSION_DEL,
      {
        id: id,
        permissionId: del[0],
      },
    );
    // 创建返回数据
    const data = bag<boolean>(a1);
    // 特殊处理
    if (isOkRes(a1)) {
      data.payload = true;
    }
    return data;
  }
  const data = bag<boolean>([]);
  return data;
}
// 权限列表
export async function getPermissionListService() {
  const result = await simpleListService<any, PermissionDto>(
    API_PERMISSION_LIST,
    {},
  );
  if (result.payload) {
    dataState.permissions = result.payload;
  }
  return result;
}
// // 编辑角色菜单
// export async function editRoleMenuService(
//   id: string,
//   have: string[],
//   selected: string[],
// ) {
//   const add = _.difference(selected, have);
//   const del = _.difference(have, selected);
//   logUtils.info("增加角色菜单", add);
//   logUtils.info("删除角色菜单", del);
//   if (add.length > 0) {
//     // 打开接口
//     const a1 = await open<ReqRoleMenuEdit, ResBoolean>(API_ROLE_MENU_ADD, {
//       roleId: id,
//       menuId: add[0],
//     });
//     // 创建返回数据
//     const data = bag<boolean>(a1);
//     // 特殊处理
//     if (isOkRes(a1)) {
//       data.payload = true;
//     }
//     return data;
//   }
//   if (del.length > 0) {
//     // 打开接口
//     const a1 = await open<ReqRoleMenuEdit, ResBoolean>(API_ROLE_MENU_DEL, {
//       roleId: id,
//       menuId: del[0],
//     });
//     // 创建返回数据
//     const data = bag<boolean>(a1);
//     // 特殊处理
//     if (isOkRes(a1)) {
//       data.payload = true;
//     }
//     return data;
//   }
//   const data = bag<boolean>([]);
//   return data;
// }
